// views/index/shop/certificate/paypage/fourth/index.js
var BASE_URL = getApp().globalData.BASE_URL
var BASE_SRC = getApp().globalData.BASE_SRC
import Notify from '../../../../../../miniprogram_npm/@vant/weapp/notify/notify';


Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag: 1,//top点击切换css
    size: '',//上传文件大小
    gzlist: "",//公正list
    list: '',//res.data数据
    id: '',//订单详情id
    value: ''//快递单号id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    var _this = this
    wx.request({
      url: `${BASE_URL}/gongzheng/jindu`,
      method: "POST",
      data: {
        id: options.id,
        type: 'xiaochengxv',
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        var str = []
        res.data.gw.forEach(itemm => {
          res.data.gz.forEach(item => {
            if (itemm.biyezheng == item.name) {
              str.push(item)
            }
          })
        })
        str.forEach((itemm) => {
          itemm.name = itemm.name.replace('公证', '扫描件')
          itemm.list = itemm.notarization.split(",")
          itemm.list.forEach((item, index) => {
            itemm.list[index] = `${BASE_URL}` + item
          })
        })
        res.data.gzimg.forEach(item => {
          item.size = Math.floor(item.size / 1024 * 100) / 100
        })
        res.data.shen.forEach(item => {
          item.size = Math.floor(item.size / 1024 * 100) / 100
        })
        res.data.weituo.forEach(item => {
          item.size = Math.floor(item.size / 1024 * 100) / 100
        })
        res.data.shouchi.forEach(item => {
          item.size = Math.floor(item.size / 1024 * 100) / 100
        })
        console.log(res, str)
        _this.setData({
          gzlist: str,
          list: res.data
        })
      }
    })
  },

  getgzdata: function () {
    var _this = this
    wx.request({
      url: `${BASE_URL}/gongzheng/jindu`,
      method: "POST",
      data: {
        id: _this.data.id,
        type: 'xiaochengxv',
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        var str = []
        res.data.gw.forEach(itemm => {
          res.data.gz.forEach(item => {
            if (itemm.biyezheng == item.name) {
              str.push(item)
            }
          })
        })
        str.forEach((itemm) => {
          itemm.name = itemm.name.replace('公证', '扫描件')
          itemm.list = itemm.notarization.split(",")
          itemm.list.forEach((item, index) => {
            itemm.list[index] = `${BASE_URL}` + item
          })
        })
        res.data.gzimg.forEach(item => {
          item.size = Math.floor(item.size / 1024 * 100) / 100
        })
        res.data.shen.forEach(item => {
          item.size = Math.floor(item.size / 1024 * 100) / 100
        })
        res.data.weituo.forEach(item => {
          item.size = Math.floor(item.size / 1024 * 100) / 100
        })
        res.data.shouchi.forEach(item => {
          item.size = Math.floor(item.size / 1024 * 100) / 100
        })

        console.log(res, str)

        _this.setData({
          gzlist: str,
          list: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  queding: function () {
    var _this = this
    if (!_this.ifdanhao(_this.data.value)) {
      Notify('快递单号错误');
    }
  },

  //下一步按钮
  down_one_foot: function () {
    var _this = this
    wx.request({
      url: `${BASE_URL}/gongzheng/imgtijiao`,
      method: "POST",
      data: {
        id: _this.data.list.list.id
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        if (res.data.status == 1) {
          console.log('成功')
          wx.reLaunch({
            url: `/views/index/shop/certificate/jindupage/index?id=${_this.data.id}`
          })
        } else {
          if (_this.ifdanhao(_this.data.value)) {
            //提交下单
            wx.request({
              url: `${BASE_URL}/gongzheng/number`,
              method: "POST",
              data: {
                id: _this.data.list.list.id,
                number: _this.data.value
              },
              header: {
                'Content-Type': 'application/json' //GET方式是这个
              },
              success(res) {
                wx.reLaunch({
                  url: `/views/index/shop/certificate/jindupage/index?id=${_this.data.id}`
                })
              }
            })
          }else{
            Notify('未上传材料或快递单号错误');
          }
        }
        console.log(res)
      }
    })
  },
  //快递单号正则
  ifdanhao: function (danhao) {
    let ifdanhao = /^[A-Za-z0-9-]{4,35}$/;
    return ifdanhao.test(danhao);
  },
  //top点击
  chuli: function (e) {
    this.setData({
      flag: e.currentTarget.dataset.flag
    })
  },
  //删除图片
  delimg: function (e) {
    var _this = this
    wx.showModal({
      title: '提示',
      content: '确定要删除吗?',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: `${BASE_URL}/gongzheng/jindudelimg`,
            method: "POST",
            data: {
              name: e.currentTarget.dataset.id,
            },
            header: {
              'Content-Type': 'application/json' //GET方式是这个
            },
            success(res) {
              _this.getgzdata()
            }
          })
        } else if (res.cancel) {}
      }
    })

  },

  upimg: function (e) {
    var _this = this
    wx.chooseMessageFile({
      count: 100,
      success(res) {
        wx.showLoading({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
        })

        res.tempFiles.forEach(item => {
          item.size = Math.floor(item.size / 1024 * 100) / 100
          console.log(item)
        })

        const tempFilePaths = res.tempFiles
        console.log(res, tempFilePaths)

        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: `${BASE_URL}/gongzheng/upload`, //仅为示例，非真实的接口地址
            filePath: tempFilePaths[i].path,
            name: 'identity',
            header: {
              "Content-Type": "multipart/form-data"
            },
            formData: {
              id: _this.data.list.list.id,
              name: e.currentTarget.dataset.name,
              t: e.currentTarget.dataset.t
            },
            success(res) {
              _this.getgzdata()
              if (res.statusCode == 200) {
                wx.hideLoading()
                wx.showToast({
                  title: "上传成功",
                  icon: "none",
                  duration: 1500
                })
              }
              var data = JSON.parse(res.data)
              console.log(data)
              if (data.status == 4) {
                wx.showToast({
                  title: `上传失败,${data.error}`,
                  icon: "none",
                  duration: 2000
                })
              }
            }
          })
        }
      }
    })
  },


  downfile: function (e) {
    wx.showLoading({
      title: '下载中...'
    })
    wx.downloadFile({
      url: e.currentTarget.dataset.url,
      success: function (res) {
        wx.hideLoading()
        wx.showToast({
          title: '下载成功',
          icon: 'success',
          duration: 2000
        })
        const tempFilePath = res.tempFilePath;
        // 保存文件
        wx.saveFile({
          tempFilePath,
          success: function (res) {
            const savedFilePath = res.savedFilePath;
            // 打开文件
            wx.openDocument({
              filePath: savedFilePath,
              success: function (res) {
                console.log('打开文档成功')
              },
            });
          },
          fail: function (err) {
            console.log('保存失败：', err)
          }
        });
      },
      fail: function (err) {
        wx.showToast({
          title: '下载失败',
          icon: 'error',
          duration: 2000
        })
        console.log('下载失败：', err);
      },
    });
  },

  //必备材料预览
  preview_img: function (e) {
    var _this = this;
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  preview_src: function (e) {
    var _this = this;
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: e.currentTarget.dataset.src // 需要预览的图片http链接列表
    })
  },


})